<?php


namespace app\project\controller;


use app\project\model\GoodsModel;
use cmf\controller\AdminBaseController;
use think\facade\Db;
use think\Request;

class GoodsController extends AdminBaseController
{
    public function index(Request $request)
    {
        $select = $request->param();
        $where = [];
        if($select){
            $where[] = ['name|zy|status','like',"%$select%"];
        }
        $res = GoodsModel::order('id','desc')
            ->where($where)
            ->limit(5)
            ->paginate();
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function addPost(Request $request)
    {
        $data = $request->param();
        $data['images'] = implode(',',$data['images']);
        if(empty($data['image']))
        {
            $this->error('图片不能为空');
        }
        if(empty($data['images']))
        {
            $this->error('图片不能为空');
        }
        if(empty($data['name']))
        {
            $this->error('图片名称不能为空');
        }
        $data['image'] = cmf_get_image_url($data['image']);
        $data['create_time'] = date('Y:m:d H:i:s');
        $res = GoodsModel::create($data);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    //是否展示
    public function show(Request $request)
    {
        $id = $request->param('id');
        if(empty($id))
        {
            $this->error('修改失败');
        }
        $res = GoodsModel::where('id','=',$id)
            ->find();
        if($res['zy'] == 0)
        {
            GoodsModel::where('id','=',$id)
                ->update(['zy'=>1]);
            $this->success('修改成功');
        }else{
            GoodsModel::where('id','=',$id)
                ->update(['zy'=>0]);
            $this->success('修改成功');
        }
    }
    public function edit(Request $request)
    {
        $id = $request->param('id');
        if(empty($id))
        {
            $this->error('修改失败');
        }
        $res = GoodsModel::where('id','=',$id)
            ->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function editPost(Request $request)
    {
        $data = $request->param();
        $data['images'] = implode(',',$data['images']);
        if(!$data['id'])
        {
            $this->error('参数错误');
        }
        if(!$data['images'])
        {
            $this->error('缺少参数');
        }
        if(!$data['name'])
        {
            $this->error('缺少参数');
        }
        $data['update_time'] = date('Y:m:d H:i:s');
        $res = GoodsModel::where('id','=',$data['id'])
            ->update($data);
        if($res)
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete(Request $request)
    {
        $id = $request->param('id/d');
        if(!$id)
        {
            $this->error('参数错误');
        }
        $res = GoodsModel::where(

            'id','=',$id)
            ->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //
    public function setGoodsAttr()
    {
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        return $this->fetch();
    }
    //
    public function GoodsAttr()
    {
        $attr = input('post.attr');
        $goods_id = input('post.goods_id');
        if(count($attr)<1){
            $this->error('至少配置一个');
        }
    halt($attr);
        GoodsModel::where('goods_id','=',$goods_id)
            ->delete();
        $insert_data = [];
        foreach ($attr as $attrs){
            $insert_data[] = [
                'goods_id'=>$goods_id,
                'attr'=>$attrs['attr']
            ];
        }
        $res = Db::name('goods_attr')
            ->insertAll($insert_data);
        if($res){
            $this->success('成功');
        }
    }
    //
    public function getGoodsAttr()
    {
        $goods_id = input('post.goods_id');
        $res = Db::name('goods_attr')
            ->where('goods_id','=',$goods_id)
            ->select();
        return json(['code'=>1,'msg'=>'数据','data'=>$res]);
    }
    //
    public function setAttr()
    {
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        return $this->fetch();
    }
    public function AttrIndex()
    {
        $goods_id = input('post.goods_id');
        halt($goods_id);
        $res = Db::name('product_attr')
            ->where('goods_id','=',$goods_id)
            ->select();
        return json(['code'=>1,'msg'=>'数据','data'=>$res]);
    }
    public function sentAttrIndex()
    {
        $attr= input('post.attr');
        $goods_id = input('post.goods_id');
        $data = [];
        foreach ($attr as $attrs){
            $data[] = [
                'goods_id'=>$goods_id,
                'attr_id'=>$attrs['attr'],
                'attr_value'=>$attrs['attr_value']
            ];
        }
        Db::name('product_attr')
            ->insertAll($data);
    }
    public function setGoods()
    {
        $goods_id = input('goods_id');
        $this->assign('goods_id',$goods_id);
        return $this->fetch();
    }
    public function GoodsAttrGet()
    {
        $attr = input('attr');
        $goods_id = input('post.goods_id');
        $attr = json_encode($attr,JSON_UNESCAPED_UNICODE);
        $res = GoodsModel::where('id','=',$goods_id)
            ->update(['attr'=>$attr]);
        if($res)
        {
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function GoodsAttrIndex()
    {
        $goods_id = input('goods_id');
        $res = GoodsModel::find($goods_id);
        $res['attr'] = json_decode($res['attr'],true);
        return json(['code'=>1,'msg'=>'数据','data'=>$res]);
    }
}